//首页模板地址
var mubanNum;
var skinNum;
var topicNum;
var dest = "zmh";
$(function() {
	//首页部分---------------------
	//首页遍历显示
	if(!CD.geturl().business) {
		window.location.href = "l-userlogin.html"
	}

	skinTo(function(data) {
		//确定背景皮肤数值
		skinNum = data.data.skinNum;
		topicNum = data.data.topitalNum;
		//首页主题的遍历
		if(data.data.topitalNum == 1) {
			mubanNum = "u-templet-one.html";
			$('#muban').attr({
				"src": mubanNum
			});
			topitalNum();
		} else if(data.data.topitalNum == 2) {
			mubanNum = "u-templet-two.html";
			$('#muban').attr({
				"src": mubanNum
			});
			topitalNum();
		} else if(data.data.topitalNum == 3) {
			mubanNum = "u-templet-three.html";
			$('#muban').attr({
				"src": mubanNum
			});
			topitalNum();
		}
	})

	//首页登录页面的显示切换
	if(CD.geturl().login == getCookieValue("token")) {
		$('.myHeader').css({
			'display': 'none'
		});
		$('.myHeaderMore').css({
			'display': 'block'
		});
	} else {
		$('.myHeader').css({
			'display': 'block'
		});
		$('.myHeaderMore').css({
			'display': 'none'
		});
	}
	//首页我的链接
	queryAllCategory(function(data) {
		$('#myNewsListIndex').html(template('myNewsListIndexTemplate', data));
		$('#myNewsListIndex li').on('tap', function() {
			var linkUrl = $(this).attr('data-url');
			window.location.href = linkUrl
		});

	});
	//首页个人信息
	heardLook(function(data) {
		//首页侧边栏用户信息
		$('#userHeader').html(template('userHeaderTemplate', data));
		//下拉顶部用户图片
		$('.myAllHeader img').attr({
			"src": data.user.userImg
		});
		//下拉用户产品服务信息
		$('.mynews p').html(data.user.userServe);
		//首页中心用户信息
		if(mubanNum == "u-templet-two.html") {
			$('#muban').contents().find('#useHeader').html(template('useHeaderTemplate', data));
		} else if(mubanNum == "u-templet-one.html") {
			$('#muban').contents().find('#userMsg').html(template('userMsgTemplate', data));
		} else if(mubanNum == "u-templet-three.html") {
			$('#muban').contents().find('#useHeader').html(template('useHeaderTemplate', data));
		}

	});
	//控制首页下拉内容的切换
	var windowH = $(window).height();
	$('#muban').css({
		'height': windowH
	});
	$('.home-contain').css({
		'top': windowH
	});
	//隐藏下拉内容
	$('#closeAll').on('tap', function() {
		$('.home-contain').stop().animate({
			'top': windowH,
			'minHeight': windowH
		});
		//初始化scroll
		mui('#contain').scroll().setTranslate(0, 0);
	});

	//滚动的距离来显示下拉内容
	var scroll = mui('#contain').scroll();
	$('#contain').bind('scroll', function(e) {　　
		var scrollY = scroll.y;
		//console.log(scrollY);
		if(scrollY < -2) {
			$('.home-contain').stop().animate({
				'top': 0,
				'minHeight': windowH
			});
		};
	});

	//首页跳转
	function jumpTo(num1, num2) {
		$(num1).on('tap', function() {
			var business = CD.geturl.business;
			window.location.href = num2 + '?business=' + CD.geturl().business;
		});
	};
	jumpTo('.changHeader img', 'g-center.html');
	//用户登录
	jumpTo('#userIndexLogin', 'l-userlogin.html');
	//创建进入
	jumpTo('.myCreateNew', 'l-login.html');
	//侧边我的名片
	jumpTo('#userListOne', 'v-store.html');
	jumpTo('#userListTwo', 'b-cards.html');
	jumpTo('#userListThree', 'm-mypacket.html');
	jumpTo('#userListFour', 'a-link.html');
	//更多
	jumpTo('#userMoreMangement', 'g-center.html');
	//退出 
	$('.userListBottom span').on('tap', function() {
		goOut();
	});

	//后台进入首页部分----------------------
	var admin = CD.geturl().look;
	console.log(admin);
	//判断是否是从后台进入;
	if(admin) {
		skinTo(function(data) {

			//后台顶部放回按钮显示
			$('.changHeader').css({
				'display': 'block'
			});
			$('#closeHeader').css({
				'display': 'none'
			});
			//禁止首页滑动出现侧边菜单
			$('.mui-inner-wrap').on('drag', function(event) {
				event.stopPropagation();
			});
			//隐藏首页下拉内容
			$('.home-contain').css({
				"display": "none"
			});

			//后台操作界面的显示
			$('.admin').css({
				"display": "block"
			});

			//后台界面切换首页背景图
			var bgNum = 1;
			//后台数据遍历皮肤值;
			var newUrl = "url(img/index-bg/u" + data.data.skinNum + ".png) no-repeat";
			$('#muban').contents().find('.main-bg').css({
				'background': newUrl,
				'backgroundSize': 'cover'
			});
			//console.log(newUrl);
			//切换皮肤
			$('.pic-l').on('tap', function() {
				bgNum--;
				if(bgNum < 1) {
					bgNum = 5;
				};
				skinNum = bgNum
				newUrl = "url(img/index-bg/u" + bgNum + ".png) no-repeat";
				$('#muban').contents().find('.main-bg').css({
					'background': newUrl,
					'backgroundSize': 'cover'
				});
			});
			$('.pic-r').on('tap', function() {
				bgNum++;
				if(bgNum > 5) {
					bgNum = 1;
				};
				skinNum = bgNum
				newUrl = "url(img/index-bg/u" + bgNum + ".png) no-repeat";
				$('#muban').contents().find('.main-bg').css({
					'background': newUrl,
					'backgroundSize': 'cover'
				});
			});

			//主题的切换
			//后台主题的遍历
			if(data.data.topitalNum == 1) {
				mubanNum = "u-templet-one.html";
				$('#muban').attr({
					"src": mubanNum
				});
				topitalNum();
			} else if(data.data.topitalNum == 2) {
				mubanNum = "u-templet-two.html";
				$('#muban').attr({
					"src": mubanNum
				});
				topitalNum();
			}
			//主题页面的显示与隐藏
			$('.titleToOpen').on('tap', function() {
				$('.myTopital').animate({
					"left": '0'
				});
			});
			$('.titleToClose').on('tap', function() {
				$('.myTopital').animate({
					"left": '100%'
				});
				//切换主题后再次出发用户信息遍历接口
				topitalNum();
			});
			//主题的首页切换
			$('.muban').on('tap', function() {
				var newTemplate = $(this).attr('data-add');

				if(newTemplate == "u-templet-one.html") {
					topicNum = 1;
				} else if(newTemplate == "u-templet-two.html") {
					topicNum = 2;
				} else if(newTemplate == "u-templet-three.html") {
					topicNum = 3;
				}
				mubanNum = newTemplate
				console.log(newTemplate);
				//模板的切换
				$('#muban').attr({
					"src": newTemplate
				});
				//点击后的状态
				$('.muban').children('.cover').css({
					"display": 'none'
				});
				$(this).children().eq(0).css({
					"display": 'block'
				});
			});

			//选择栏目的显现
			$('.slide .img').on('tap', function() {
				$('.myContain').css({
					'display': 'block'
				});
			});

			//保存
			$('.topitalTo').on('tap', function() {
				console.log(skinNum, 1021)
				console.log(topicNum, 1252)
				topitalTo({
					skinNum: skinNum,
					topicNum: topicNum,
					token: getCookieValue("token")
				}, function(data) {
					if(data.code == 200) {
						mui.toast('修改成功!')
					}
				});
			});

		});
	};

	//文章
	article(function(data) {
		$('#myarticle-list').html(template('myarticle-list-template', data));
		$("#myarticle-list > li").on('tap', function() {
			var articalId = $(this).attr('data-id');
			
			console.log(articalId, 123456)
			window.location.href = "page_text_details.html?url=" + articalId + "&c_user_id=" + CD.geturl().business + "&dest=" + dest;;
		})
	});
//	articleTwo(function(data) {
//		$('#myarticle-list-two').html(template('myarticle-list-template-two', data));
//		$("#myarticle-list-two > li").on('tap', function() {
//			var articalId = $(this).attr('data-id');
//			window.location.href = articalId;
//		})
//	});

})
var articleTwo = function(callback) {
	mui.ajax(window.address + '/wechat/singlepage/getArticle', {
		data: {
			user_uuid: CD.geturl().business,
			page: 1,
			rows: 10,
		},
		type: 'get',
		dataType: 'json',
		success: function(data) {
			console.log(data,123456);
			callback && callback(data)
		}
	})
}

//退出
var goOut = function(callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		url: window.address + "/user/logout",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			'token': getCookieValue("token")
		},
		dataType: "json",
		success: function(data) {
			window.location.href = 'l-userlogin.html'
		}
	});
}

//微链接
var queryAllCategory = function(callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/link/selectByUuid",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			'userUuid': CD.geturl().business
		},
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
};

//个人信息
var heardLook = function(callback) {
	$.ajax({
		type: "post",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/user/findBayUserUuid",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			"userUuid": CD.geturl().business
		},
		success: function(data) {
			console.log(data, 88);
			callback && callback(data);
		}
	});
}

//主题皮肤接口
var skinTo = function(callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/userTheme/listUserTheme",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			"token": getCookieValue("token"),
			"userUuid": CD.geturl().business
		},
		success: function(data) {
			console.log(data, 99);
			callback && callback(data);
		}
	});
}

//切换主题后再次出发用户信息遍历接口
var topitalNum = function() {

	heardLook(function(data) {
		//首页中心用户信息
		if(mubanNum == "u-templet-two.html") {
			$('#muban').contents().find('#useHeader').html(template('useHeaderTemplate', data));

		} else if(mubanNum == "u-templet-one.html") {
			$('#muban').contents().find('#userMsg').html(template('userMsgTemplate', data));

		} else if(mubanNum == "u-templet-three.html") {
			$('#muban').contents().find('#useHeader').html(template('useHeaderTemplate', data));
		}
		skinTo(function(data) {
			//首页数据遍历皮肤值;
			var newUrl = "url(img/index-bg/u" + data.data.skinNum + ".png) no-repeat";
			$('#muban').contents().find('.main-bg').css({
				'background': newUrl,
				'backgroundSize': 'cover'
			});
			if(mubanNum == "u-templet-two.html") {
				$('#muban').contents().find('#slide').html(template('slideTemplateTwo', data));
				//点击跳转
				$('#muban').contents().find('#slide > div').click(function() {
					var toUrl = $(this).attr('data-url');
					window.location.href = toUrl;
				})
			} else if(mubanNum == "u-templet-one.html") {
				$('#muban').contents().find('#slide').html(template('slideTemplate', data));
				//点击跳转
				$('#muban').contents().find('#slide > div').click(function() {
					var toUrl = $(this).attr('data-url');
					window.location.href = toUrl;
				})
			} else if(mubanNum == "u-templet-three.html") {
				$('#muban').contents().find('#slide').html(template('slideTemplateThree', data));
				//点击跳转
				$('#muban').contents().find('#slide > .contain').click(function() {
					var toUrl = $(this).attr('data-url');
					window.location.href = toUrl;
				})
			}

			//主题内容的选择
			var adminTwo = CD.geturl().look;
			if(adminTwo) {
				$('#muban').contents().find('#slide > div').on('tap', function() {
					var thisId = $(this).attr('data-id');
					var linkId = $(this).attr('data-cid');
					$('.myContain').css({
						'display': 'block'
					});

					//关闭主题内容
					//获取当前所需的替换信息
					var linkUrl;
					var linkImg;
					var linkName;

					$('.containClose').on('tap', function() {
						$('.myContain').css({
							'display': 'none'
						});
						window.location.reload();
					});

					$('.containToChooseOne').click(function() {
						$('.containToChooseTwoChange').css({
							'display': 'none'
						});
						$('.containToChooseOneChange').css({
							'display': 'block'
						});
						$(this).css({
							"backgroundColor": "#007AFF",
							"color": "white",
							"border": "1px solid #007AFF"
						}).siblings().css({
							"backgroundColor": "white",
							"color": "black",
							"border": "1px solid #cccccc"
						});

						linkUrl = 0;
						linkImg = 0;
						linkName = 0;
						$('.containLinkList').css({
							"border": "none"
						});
					});
					$('.containToChooseTwo').click(function() {
						$('.containToChooseOneChange').css({
							'display': 'none'
						});
						$('.containToChooseTwoChange').css({
							'display': 'block'
						});
						$(this).css({
							"backgroundColor": "#007AFF",
							"color": "white",
							"border": "1px solid #007AFF"
						}).siblings().css({
							"backgroundColor": "white",
							"color": "black",
							"border": "1px solid #cccccc"
						});
						linkUrl = 0;
						linkImg = 0;
						linkName = 0;
						$('.containVstore').css({
							"border": "none"
						});
					});

					$('.containVstore').click(function() {
						$(this).css({
							"border": "1px solid red"
						});
						linkUrl = "v-shop.html?business=" + CD.geturl().business;
						linkImg = $(this).children().children().attr('class');
						linkName = $(this).children().eq(1).html();

					});

					//微链接
					queryAllCategory(function(data) {
						$('#containLink').html(template('containLinkTemplate', data));

						$('.containLinkList').click(function() {
							$(this).css({
								"border": "1px solid red"
							}).siblings().css({
								"border": "none"
							});
							linkUrl = $(this).attr('data-url');
							linkImg = $(this).children().eq(0).attr('class');
							linkName = $(this).children().eq(1).html();
						});
					})

					$('#containBtn').click(function() {
						if(!linkUrl) {
							mui.toast('所选不能为空!');
							return false;
						}
						if(!linkImg) {
							mui.toast('所选不能为空!');
							return false;
						}
						if(!linkName) {
							mui.toast('所选不能为空!');
							return false;
						}
						console.log(linkUrl, "跳转地址");
						console.log(linkImg, "imageUrl图片地址");
						console.log(linkName, "containName名字");
						console.log(thisId, "number的编号");
						console.log(linkId, "id产品id编号");
						topitalContainModify({
							id: linkId,
							imageUrl: linkImg,
							containName: linkName,
							numbers: thisId,
							linkUrl: linkUrl
						}, function(data) {
							mui.toast('保存成功')
						})

					});
				});
			}

		});

	});
}

//主题皮肤接口修改
var topitalTo = function(params, callback) {
	$.ajax({
		type: "post",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/userTheme/updateUserTheme",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		success: function(data) {
			console.log(data, 99);
			callback && callback(data);
		}
	});
}

//主题内容修改
var topitalContainModify = function(params, callback) {
	$.ajax({
		type: "post",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/userTheme/updateContain",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		success: function(data) {
			console.log(data, 888888);
			callback && callback(data);
		}
	});
}
//文章
var article = function(callback) {
	$.ajax({
		type: "get",
		url: window.address + "/wechat/singlepage/getTopPageList",
		data: {
			page: 1,
			rows: "20",
			c_user_id: CD.geturl().business
		},
		async: false,
		success: function(data) {
			console.log(data, 101);
			callback && callback(data);
		}
	});
}